/*
 * The JAnalytics Library provides a Java API to the Google Analytics service.
 * Copyright (C) 2007 Ansir Development Limited (http://www.ansir.ca)
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

package ca.ansir.analytics.tag;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import ca.ansir.analytics.reports.JAnalytics;
import ca.ansir.analytics.reports.ReportTypes;

/**
 * The <code>ReportTypeSelectTag</code> class is used to write out a select
 * tag for the available report types. This tag is bound to the example web
 * application via the Tag Library Definition (janalytics.tld) located in the
 * example/WEB-INF directory.
 * 
 * @author Dan Andrews
 */
public class ReportTypeSelectTag extends TagSupport {

	/** Generated serialVersionUID. */
	private static final long serialVersionUID = 5216833089675159622L;

	/** The default name for the <code>ReportTypeSelect</code> is "vid". */
	private String name;

	/** Optional style attribute. */
	private String style;

	/** Optional onchange attribute.. */
	private String onchange;

	/** By default the xhtml attribute is set to false */
	private String xhtml;

	/** Optional size attribute. */
	private String size;

	/** By default the selected option is the Executive Overview. */
	private String selectedoption = ReportTypes.DASHBOARD_REPORT
			.getValueAsString();

	/**
	 * Constructor.
	 */
	public ReportTypeSelectTag() {
		init();
	}

	/**
	 * Initialize and re-initialize.
	 */
	private void init() {
		name = JAnalytics.REPORT_TYPE_ATTRIBUTE_NAME;
		style = "";
		onchange = "";
		xhtml = "false";
		size = "";
	}

	/**
	 * Process the start of the tag.
	 * 
	 * @return The SKIP_BODY constant.
	 */
	public int doStartTag() {
		init();
		return Tag.SKIP_BODY;
	}

	/**
	 * Process the end of the tag.
	 * 
	 * @return The SKIP_BODY constant.
	 */
	public int doEndTag() throws JspException {
		writeTag();
		return Tag.SKIP_BODY;
	}

	/**
	 * Writes the out the report type select as html.
	 * 
	 * @param writer
	 *            The JspWriter to write to.
	 * @throws JspException
	 */
	private void writeTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		StringBuffer buf = new StringBuffer("<select name=\"" + getName()
				+ "\"");
		if (getId() != null && !"".equals(getId())) {
			buf.append(" id=\"" + getId() + "\"");
		}
		if (getSize() != null && !"".equals(getSize())) {
			buf.append(" size=\"" + getSize() + "\"");
		}
		if (getStyle() != null && !"".equals(getStyle())) {
			buf.append(" style=\"" + getStyle() + "\"");
		}
		if (getOnchange() != null && !"".equals(getOnchange())) {
			buf.append(" onchange=\"" + getOnchange() + "\"");
		}
		buf.append(">\n");
		for (Iterator iterator = ReportTypes.getReportTypes(); iterator
				.hasNext();) {
			ReportTypes rt = (ReportTypes) iterator.next();
			buf.append("\t<option value=\"").append(rt.getValueAsString())
					.append("\"");
			if (rt.getValueAsString().equals(getSelectedoption())) {
				if (Boolean.valueOf(getXhtml()).booleanValue()) {
					buf.append(" selected=\"selected\"");
				} else {
					buf.append(" selected");
				}
			}
			buf.append(">").append(rt.getReportName()).append("</option>\n");
		}
		buf.append("</select>");
		try {
			writer.println(buf.toString());
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}
	}

	/**
	 * Gets the optional name (default is name="vid").
	 * 
	 * @return The optional name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the optional name (default is name="vid").
	 * 
	 * @param name
	 *            The optional name.
	 */
	public void setName(String name) {
		if (name == null || "".endsWith(name)) {
			throw new IllegalArgumentException("Name is typically equal "
					+ "to 'vid' and cannot be empty.");
		}
		this.name = name;
	}

	/**
	 * Gets the optional onchange attribute.
	 * 
	 * @return The optional onchange attribute.
	 */
	public String getOnchange() {
		return onchange;
	}

	/**
	 * Sets the optional onchange attribute.
	 * 
	 * @param onchange
	 *            The optional onchange attribute.
	 */
	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	/**
	 * Gets the optional selectedoption attribute (default is selectedoption="<%=
	 * ReportTypes.EXECUTIVE_OVERVIEW .getValueAsString()%>)."
	 * 
	 * @return The optional selectedoption attribute.
	 */
	public String getSelectedoption() {
		return selectedoption;
	}

	/**
	 * Sets the optional selectedoption attribute (default is selectedoption="<%=
	 * ReportTypes.EXECUTIVE_OVERVIEW .getValueAsString()%>)."
	 * 
	 * @param selectedoption
	 *            The optional selectedoption attribute.
	 */
	public void setSelectedoption(String selectedoption) {
		// Will throw IllegalArgumentException for us.
		ReportTypes.fromInt(Integer.parseInt(selectedoption));
		this.selectedoption = selectedoption;
	}

	/**
	 * Gets the optional size attribute.
	 * 
	 * @return The optional size attribute.
	 */
	public String getSize() {
		return size;
	}

	/**
	 * Sets the optional size attribute.
	 * 
	 * @param size
	 *            The optional size attribute.
	 */
	public void setSize(String size) {
		int num = Integer.parseInt(size);
		if (num <= 0) {
			throw new IllegalArgumentException(
					"Size must be greater than zero.");
		}
		this.size = size;
	}

	/**
	 * Gets the optional style attribute.
	 * 
	 * @return The optional style attribute.
	 */
	public String getStyle() {
		return style;
	}

	/**
	 * Sets the optional style attribute.
	 * 
	 * @param style
	 *            The style attribute.
	 */
	public void setStyle(String style) {
		this.style = style;
	}

	/**
	 * Gets the optional xhtml attribute (default is xhtml="false").
	 * 
	 * @return The optional xhtml attribute.
	 */
	public String getXhtml() {
		return xhtml;
	}

	/**
	 * Sets the optional xhtml attribute (default is xhtml="false").
	 * 
	 * @param xhtml
	 *            The optional xhtml attribute.
	 */
	public void setXhtml(String xhtml) {
		this.xhtml = xhtml;
	}

}
